using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using TeleTwitter.Lib;

namespace WpfTeleTwitter
{
	public class StatusControl : Control
	{
		public static readonly RoutedUICommand DirectReplyCommand = new RoutedUICommand("Direct Reply", "DirectReply", typeof(StatusControl));
		public static readonly RoutedUICommand PublicReplyCommand = new RoutedUICommand("Puplic Reply", "PublicReply", typeof(StatusControl));

		static StatusControl()
		{
			//This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
			//This style is defined in themes\generic.xaml
			DefaultStyleKeyProperty.OverrideMetadata(typeof (StatusControl),
			                                         new FrameworkPropertyMetadata(typeof (StatusControl)));

			CommandBinding cmdBinding = new CommandBinding(DirectReplyCommand, HandleDirectReply);
			CommandManager.RegisterClassCommandBinding(typeof(StatusControl), cmdBinding);
			cmdBinding = new CommandBinding(PublicReplyCommand, HandlePublicReply);
			CommandManager.RegisterClassCommandBinding(typeof(StatusControl), cmdBinding);
		}

		public static void HandleDirectReply(object target, ExecutedRoutedEventArgs e)
		{
			StatusControl statusControl = (StatusControl)target;
			System.Diagnostics.Debugger.Break();
		}

		public static void HandlePublicReply(object target, ExecutedRoutedEventArgs e)
		{
			StatusControl statusControl = (StatusControl)target;
			System.Diagnostics.Debugger.Break();
		}

		public Status Status
		{
			get { return (Status) GetValue(StatusProperty); }
			set { SetValue(StatusProperty, value); }
		}

		public static DependencyProperty StatusProperty =
			DependencyProperty.Register(
				"Status",
				typeof (Status),
				typeof (StatusControl),
				new PropertyMetadata(
					null,
					OnStatusInvalidated));

		public static readonly RoutedEvent StatusChangedEvent =
			EventManager.RegisterRoutedEvent(
				"StatusChanged",
				RoutingStrategy.Bubble,
				typeof (RoutedPropertyChangedEventHandler<DateTime>),
				typeof (StatusControl));

		protected virtual void OnStatusChanged(Status oldValue, Status newValue)
		{
			RoutedPropertyChangedEventArgs<Status> args = new
				RoutedPropertyChangedEventArgs<Status>(oldValue, newValue);
			args.RoutedEvent = StatusChangedEvent;
			RaiseEvent(args);
		}

		private static void OnStatusInvalidated(DependencyObject d,
		                                        DependencyPropertyChangedEventArgs e)
		{
			StatusControl clock = (StatusControl) d;
			Status oldValue = (Status) e.OldValue;
			Status newValue = (Status) e.NewValue;
			clock.OnStatusChanged(oldValue, newValue);
		}
	}
}